//
//  GroupTopTabView.m
//  Waiguofang
//
//  Created by waiguofang on 14-11-17.
//
//

#import "GroupTopTabView.h"
#import "UIColor+Additions.h"
#import "UIView+RTLayout.h"

@interface GroupTopTabViewCell : UIView

@property (nonatomic, strong) UILabel *labelCell;
@property (nonatomic, strong) UIButton *buttonCell;
@property (nonatomic, strong) UIView *line;

- (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title;
- (void)setCellSelected:(BOOL)selected;
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

@end

@implementation GroupTopTabViewCell
@synthesize labelCell,buttonCell,line;

- (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.buttonCell = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.buttonCell setFrame:self.bounds];
        [self.buttonCell setBackgroundColor:[UIColor clearColor]];
        
        self.labelCell = [[UILabel alloc] initWithFrame:self.bounds];
        [self.labelCell setBackgroundColor:[UIColor clearColor]];
        [self.labelCell setText:title];
        [self.labelCell setTextAlignment:NSTextAlignmentCenter];
        [self.labelCell setFont:[UIFont fontWithName:@"TrebuchetMS" size:15]];
        
        self.line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-2, self.frame.size.width, 1)];
        
        [self addSubview:self.buttonCell];
        [self addSubview:self.labelCell];
        [self addSubview:self.line];
    }
    return self;
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    [self.buttonCell addTarget:target action:action forControlEvents:controlEvents];
}

- (void)setCellSelected:(BOOL)selected
{
    if (selected) {
        [self.labelCell setTextColor:[UIColor redColor]];
        [self.line setBackgroundColor:[UIColor redColor]];
    }
    else
    {
        [self.labelCell setTextColor:[UIColor grayColor]];
        [self.line setBackgroundColor:[UIColor clearColor]];
    }
}

@end

@interface GroupTopTabView()

@property (nonatomic, strong) GroupTopTabViewCell *pastGroup;
@property (nonatomic, strong) GroupTopTabViewCell *currentGroup;
@property (nonatomic, strong) GroupTopTabViewCell *futureGroup;

@property (nonatomic, assign) GroupType groupType;

@end

@implementation GroupTopTabView
@synthesize pastGroup,currentGroup,futureGroup;

- (id)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    
        CGSize size = CGSizeMake(self.frame.size.width/3, self.frame.size.height);
        
        self.pastGroup = [[GroupTopTabViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height) withTitle:[titleArray objectAtIndex:0]];
        [pastGroup addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.currentGroup = [[GroupTopTabViewCell alloc] initWithFrame:CGRectMake(size.width, 0, size.width, size.height) withTitle:[titleArray objectAtIndex:1]];
       [currentGroup addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.futureGroup = [[GroupTopTabViewCell alloc] initWithFrame:CGRectMake(size.width*2, 0, size.width, size.height) withTitle:[titleArray objectAtIndex:2]];
        [futureGroup addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [currentGroup setCellSelected:YES];
        [pastGroup setCellSelected:NO];
        [futureGroup setCellSelected:NO];
        
        UIView *lineMid01 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 15)];
        [lineMid01 setBackgroundColor:[UIColor colorWithHex:0xDEDEDE alpha:1]];
        
        UIView *lineMid02 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 15)];
        [lineMid02 setBackgroundColor:[UIColor colorWithHex:0xDEDEDE alpha:1]];
        
        lineMid01.left = self.pastGroup.right;
        lineMid02.right = self.futureGroup.left;
        lineMid01.centerY = size.height/2;
        lineMid02.centerY = size.height/2;
        
        [self addSubview:pastGroup];
        [self addSubview:currentGroup];
        [self addSubview:futureGroup];
        [self addSubview:lineMid01];
        [self addSubview:lineMid02];
        
        [[self layer] setBorderWidth:1];
        [[self layer] setBorderColor:[UIColor getColorFromRed:222 Green:222 Blue:222 Alpha:255]];
    }
    return self;
}

- (void)setSelectedIndex:(GroupType)typeIndex
{
    if (typeIndex==GroupTypePast) {
        [currentGroup setCellSelected:NO];
        [pastGroup setCellSelected:YES];
        [futureGroup setCellSelected:NO];
        self.groupType = GroupTypePast;
    }
    else if(typeIndex==GroupTypeCurrent)
    {
        [currentGroup setCellSelected:YES];
        [pastGroup setCellSelected:NO];
        [futureGroup setCellSelected:NO];
        self.groupType = GroupTypeCurrent;
    }
    else
    {
        [currentGroup setCellSelected:NO];
        [pastGroup setCellSelected:NO];
        [futureGroup setCellSelected:YES];
        self.groupType = GroupTypeFuthur;
    }
}

- (void)btnAction:(id)sender
{
    UIButton *btnSender = (UIButton *)sender;
    if ([[btnSender superview] isEqual:pastGroup]) {
        [currentGroup setCellSelected:NO];
        [pastGroup setCellSelected:YES];
        [futureGroup setCellSelected:NO];
        self.groupType = GroupTypePast;
    }
    else if ([[btnSender superview] isEqual:currentGroup])
    {
        [currentGroup setCellSelected:YES];
        [pastGroup setCellSelected:NO];
        [futureGroup setCellSelected:NO];
        self.groupType = GroupTypeCurrent;
    }
    else
    {
        [currentGroup setCellSelected:NO];
        [pastGroup setCellSelected:NO];
        [futureGroup setCellSelected:YES];
        self.groupType = GroupTypeFuthur;
    }
    
    [self.delegate cellDidSelected:self.groupType];
}

@end
